﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Entities;
using Web.Models;

namespace Web.Controllers
{
    public class LogController : Controller
    {
        public const int PageSize = 12;
        public ActionResult Log()
        {
            var logs = new PagedData<LogLine>() { Data = new List<LogLine>() };
            using (var ctx = new Web.Data.NetworkMonitorEntities())
            {
                List<Web.Data.LogResult> list = ctx.LogResult
                                                   .OrderByDescending(d => d.date)
                                                   .Take(PageSize)
                                                   .ToList();
                foreach (var item in list)
                {
                    logs.Data.Add(new LogLine { Date = item.date, MsgContent = item.msgContent, MsgType = item.msgType });
                    logs.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.LogResult.Count() / PageSize));
                }
            }
            return View(logs);
        }
        public ActionResult LogDataPage(int page)
        {
            var logs = new PagedData<LogLine>() { Data = new List<LogLine>()};
            using (var ctx = new Web.Data.NetworkMonitorEntities())
            {
                List<Web.Data.LogResult> list = ctx.LogResult
                                                   .OrderByDescending(d => d.date)
                                                   .Skip(PageSize * (page - 1))
                                                   .Take(PageSize).ToList();
                foreach (var item in list)
                {
                    logs.Data.Add(new LogLine { Date = item.date, MsgContent = item.msgContent, MsgType = item.msgType});
                    logs.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.LogResult.Count() / PageSize));
                    logs.CurrentPage = page;
                }
            }
            return PartialView(logs);
        }
    }
}
